package com.zrrd.learn.chapter07字符串;

import java.util.Random;

/**
 * 字符串综合习题
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/18 10:47
 */

public class StringComprehensiveExercise01 {

    /**
     * 字符串转化（压缩） “aabbccddee” -> “a2b2c2d2e2”
     *
     * @param str
     */
    public static String question01(String str) {

        System.out.println("第一题");
        String str1 = "";
        for (int i = 0; i < str.length(); i++) {
            if (str1.indexOf(str.charAt(i)) == -1) {
                str1 += str.charAt(i);
            }
        }
        String str2 = "";
        for (int i = 0; i < str1.length(); i++) {
            int sum = 0;
            for (int j = 0; j < str.length(); j++) {
                if (str1.charAt(i) == str.charAt(j)) {
                    sum++;
                }
            }
            str2 += str1.charAt(i) + "" + sum;
        }
        return str2;
    }

    /**
     * 给定一个长度,随机产生一个该长度的字符串,由大写,小写字母
     *
     * @param len
     */
    public static String question02(int len) {

        System.out.println("第二题");
        String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String str = "";
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            str+=letter.charAt(random.nextInt(letter.length()));
        }
        System.out.println("生成的随机字符串为\n"+str);
        return str;
    }

    /**
     * 编写程序将“jdk”全部变为大写，并输出到屏幕，截取子串“DK”，并输出到屏幕
     *
     * @param str
     */
    public static void question03(String str) {

        System.out.println("第三题");
        String str1 = str.toUpperCase();
        String str2 = str1.substring(1, 3);
        System.out.println(str2);
    }

    /**
     * 编写一个程序，将下面的一段文本中的各个单词的字母顺序翻转
     * <p>
     * “To be or not to and”,变成“oT eb ro ton ot dna”
     *
     * @param str
     */
    public static void question04(String str) {

    }

    /**
     * 编写程序将String类型字符串“test”变为“tset”
     *
     * @param str
     */
    public static void question05(String str) {

        System.out.println("第五题");
        char[] value = str.toCharArray();
        for (int i = 0; i < value.length / 2; i++) {
            char temp = value[i];
            value[i] = value[value.length - 1 - i];
            value[value.length - 1 - i] = temp;
        }
        String newstr = new String(value);
        System.out.println(newstr);

    }

    public static void main(String[] args) {


        System.out.println(question01("aabbccddee"));

        question02(10);

        question03("jdk");

        question05("test");

    }
}
